package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.OrderBean;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("order")
public class OrderController {
    @Reference
    private OrderService orderService;

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return orderService.findPage(queryPageBean);
    }

    @RequestMapping("deleteById")
    public Result deleteById(Integer id) {
        return orderService.deleteById(id);
    }

    @RequestMapping("update")
    public Result update(@RequestBody OrderBean orderBean) {
        Date date = orderBean.getOrderDate();
        if (date.compareTo(new Date()) <= 0) {
            return Result.fail("请选择今天之后的日期!");
        }
        return orderService.updateOrder(orderBean);
    }

    @RequestMapping("add")
    public Result add(@RequestBody OrderBean orderBean) {
        Date date = orderBean.getOrderDate();
        if (date.compareTo(new Date()) <= 0) {
            return Result.fail("请选择今天之后的日期!");
        }
        orderBean.setOrderType("电话预约");
        return orderService.submitOrder(orderBean);
    }

    @RequestMapping("findOrderById")
    public Result findOrderById(Integer id) {
        System.out.println(id);
        return orderService.findOrderBeanById(id);
    }

}
